package com.cheksin.controller;

import com.cheksin.dao.HelloListDao;
import com.cheksin.entity.TestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/test")
/* 类注解 */
@Api(value = "测试数据库类")
public class HelloTestController {

    @Autowired
    HelloListDao helloListDao;

    @GetMapping("/query")
    /* 方法注解 */
    @ApiOperation(value = "测试连接数据库查询方法", notes = "")
    public Object queryAll( /* 参数注解 */ @ApiParam(value = "测试参数名称", required = true) @RequestParam String name) {
        System.out.println(name);
        List<TestUser> list = helloListDao.queryAll();

        return list;

    }

    @PutMapping("/insert")
    /* 方法注解 */
    @ApiOperation(value = "测试新增数据库增加方法", notes = "")
    public Object insertUser( /* 参数注解 */ @ApiParam(value = "测试参数名称", required = true) @RequestBody TestUser testUser) {
        System.out.println(testUser.toString());
        helloListDao.insertUser(testUser);

        return "新增成功";

    }

    @PutMapping("/update")
    /* 方法注解 */
    @ApiOperation(value = "测试修改数据库方法", notes = "")
    public Object updateUser( /* 参数注解 */ @ApiParam(value = "测试参数名称", required = true) @RequestBody TestUser testUser) {
        System.out.println(testUser.toString());
        helloListDao.updateUser(testUser);

        return "修改成功";

    }

    @DeleteMapping("/delete")
    /* 方法注解 */
    @ApiOperation(value = "测试删除方法", notes = "")
    public Object deleteUser( /* 参数注解 */ @ApiParam(value = "测试参数名称", required = true) @RequestParam int id) {
        System.out.println(id);
        try {
            helloListDao.deleteUser(id);
        } catch (Exception e) {
            System.out.println(e);
        }

        return "删除成功";

    }
}
